package version_1;

import java.util.Scanner;

/**
 * @author 王子铮 11923040205
 */
public class Main {
    public static void main(String[] args) {
        //存储棋子的二维数组
        int[][] map = new int[3][3];
        Scanner input = new Scanner(System.in);
        int row;
        int column;
        //第一次绘制棋盘
        printMap(map);
        //记录步数
        int step = 0;
        //进入循环
        while (true) {
            row = -1;
            column = -1;
            //如果该位置有棋子则一直在循环中，没有棋子才可下一步走棋
            while (isFilled(map, row, column)) {
                System.out.println("输入playerX的横坐标：");
                row = input.nextInt() - 1;
                System.out.println("输入playerX的纵坐标：");
                column = input.nextInt() - 1;
            }
            step++;
            //3代表X的棋子
            map[row][column] = 3;
            //绘制棋盘
            printMap(map);
            //判断是否决出胜负，或平局
            if (ifWin(map) == 1) {
                System.out.println();
                System.out.println("X win!");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }
            if (ifWin(map) == 2) {
                System.out.println();
                System.out.println("O win!");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }
            if (stepOver(step)) {
                System.out.println();
                System.out.println("平局！");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }

            row = -1;
            column = -1;
            while (isFilled(map, row, column)) {
                System.out.println("输入playerO的横坐标：");
                row = input.nextInt() - 1;
                System.out.println("输入playerO的纵坐标：");
                column = input.nextInt() - 1;
            }
            step++;
            //5代表O的棋子
            map[row][column] = 5;
            printMap(map);
            if (ifWin(map) == 1) {
                System.out.println();
                System.out.println("X win!");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }
            if (ifWin(map) == 2) {
                System.out.println();
                System.out.println("O win!");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }
            if (stepOver(step)) {
                System.out.println();
                System.out.println("平局！");
                System.out.println();
                System.out.println("游戏结束");
                System.exit(0);
            }
        }
    }

    /**
     * 绘制棋盘
     */
    public static void printMap(int[][] map) {
        System.out.println("-------------");
        for (int i = 0; i < 3; i++) {
            System.out.print("| ");
            if (map[0][i] == 3) {
                System.out.print("X ");
            } else if (map[0][i] == 5) {
                System.out.print("O ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println("|");
        System.out.println("-------------");
        for (int i = 0; i < 3; i++) {
            System.out.print("| ");
            if (map[1][i] == 3) {
                System.out.print("X ");
            } else if (map[1][i] == 5) {
                System.out.print("O ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println("|");
        System.out.println("-------------");
        for (int i = 0; i < 3; i++) {
            System.out.print("| ");
            if (map[2][i] == 3) {
                System.out.print("X ");
            } else if (map[2][i] == 5) {
                System.out.print("O ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println("|");
        System.out.println("-------------");
    }


    /**
     * 判断当前位置是否存在棋子
     */
    public static boolean isFilled(int[][] map, int row, int column) {
        if (row == -1) {
            return true;
        }
        if (map[row][column] != 0) {
            System.out.println("请重新输入! 该位置已有棋子！");
            System.out.println();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过步数判断是否平局
     * 第九步仍未决出胜负即为平局
     */
    public static boolean stepOver(int step) {
        return step == 9;
    }

    /**
     * 判断胜负的方法
     * 1为X赢
     * 2为O赢
     * 0为未决出胜负
     */
    public static int ifWin(int[][] map) {
        int sum = 0;

        //按行判断
        for (int i = 0; i < 3; i++) {
            sum = 0;
            for (int j = 0; j < 3; j++) {
                sum += map[i][j];
                if (sum == 9) {
                    return 1;
                }
                if (sum == 15) {
                    return 2;
                }
            }
        }

        //按列判断
        for (int i = 0; i < 3; i++) {
            sum = 0;
            for (int j = 0; j < 3; j++) {
                sum += map[j][i];
                if (sum == 9) {
                    return 1;
                }
                if (sum == 15) {
                    return 2;
                }
            }
        }

        //按对角线判断
        if (map[0][0] + map[1][1] + map[2][2] == 9) {
            return 1;
        }
        if (map[0][0] + map[1][1] + map[2][2] == 15) {
            return 2;
        }
        if (map[0][2] + map[1][1] + map[2][0] == 9) {
            return 1;
        }
        if (map[0][2] + map[1][1] + map[2][0] == 15) {
            return 2;
        }
        return 0;
    }
}